#include<stdio.h>
#include<unistd.h>//exec函数头文件
#include<stdlib.h>
#include<assert.h>
#include<sys/types.h>
#include<sys/wait.h>

int main()
{
    printf("Process is running......\n");

    //一般让子进程去执行另外的程序
    pid_t id=fork();
    assert(id!=-1);

    if(id==0)
    {
        //child
        sleep(1);
        //execl("/usr/bin/ls","ls","-a","-l","--color=auto",NULL);
        //execlp("ls","ls","-a","-l","--color=auto",NULL);
        
        //char* const argv_[]={"ls","-a","-l","--color=auto",NULL};
        //execv("/usr/bin/ls",argv_);
        //execvp("ls",argv_);
        
        //运行自己的程序
        execl("./mybin","mybin",NULL);
        exit(1);//如果exec函数有返回值，那么一定出错
    }
    //parent
    int status=0;
    pid_t ret=waitpid(id,&status,0);
    if(ret>0)
    {
        printf("wait success:exit code: %d, sig: %d\n",(status>>8)&0xFF,status&0x7F);
    }

    //execl("/usr/bin/ls","ls","--color=auto","-a","-l",NULL);
    //execl("/usr/bin/topdddd","top",NULL);//为什么exec调用成功不做任何返回，只有调用错误的时候才返回-1？
    //所以调用成功的返回值，用不上，毫无意义。即如果exec函数只要返回了，就一定出错。
    //perror("exec failed\n");
    //exit(1);

    printf("Process is over......\n");//exec函数执行完后，代码已全部被新程序替换，所以printf没有被执行

    return 0;
}
